package evaluacredito.web;

import static org.junit.Assert.fail;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import evaluacredito.dao.UsuarioDAO;
import evaluacredito.dao.UsuarioDAOImpl;
import evaluacredito.excepcion.DAOExcepcion;
import evaluacredito.excepcion.LoginExcepcion;
import evaluacredito.modelo.Usuario;

/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UsuarioDAOImpl usuarioDAOImpl;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub

		String nomUsu = request.getParameter("nomUsu");
		String clave = request.getParameter("clave");
		
		try {
			//usuarioDAOImpl.validar("Jose", "123456");

//			Usuario usuario = new Usuario();
//			usuario.setNomUsu(nomUsu);
//			usuario.setClave(clave);
			
			usuarioDAOImpl.validar(nomUsu,clave);
//			
////			HttpSession session = request.getSession();
////			session.setAttribute("USUARIO_ACTUAL", usuario);
			
			response.sendRedirect("/WSEvalua_CreditoServer/principal.jsp");

			return;
		} catch (DAOExcepcion e) {
			System.err.println("Error");

			RequestDispatcher rd = request.getRequestDispatcher("/WSEvalua_CreditoServer/error.jsp");
			rd.forward(request, response);
		} catch (LoginExcepcion e) {
			System.err.println("Error");
			RequestDispatcher rd = request
					.getRequestDispatcher("/WSEvalua_CreditoServer/error_login.jsp");
			rd.forward(request, response);
		}

	}
}
